﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using DAL;
using System.Configuration;
using System.Data;

public partial class Page_MeasurementInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MembershipUser user = Membership.GetUser();
            Guid userid = (Guid)user.ProviderUserKey;
            UserExData f = UserExDataDBOption.SelectByUserID(userid);
            if (f != null)
            {
                SystemFrameworkData sfd = SystemFrameworkDataDBOption.SelectByID(f.UserLevelID);
                TreeNode xt_tn = new TreeNode(sfd.Name);
                xt_tn.SelectAction = TreeNodeSelectAction.None;
                AddFrameLevel(xt_tn, sfd.FrameLevel + 1, sfd.ID);
                TreeView1.Nodes.Add(xt_tn);
            }
            else
            {
                if (user.UserName == "wangf0228")//超级管理员
                {

                    TreeNode xt_tn = new TreeNode("系统平台", Guid.Empty.ToString());//, "", "", "~/Page/SystemFramework.aspx?id="+Guid.Empty.ToString(), "_parent");
                    xt_tn.SelectAction = TreeNodeSelectAction.None;
                    AddFrameLevel(xt_tn, 1, Guid.Empty);
                    TreeView1.Nodes.Add(xt_tn);
                }
            }
        }
        if (HiddenField1.Value == null || HiddenField1.Value == "")//没有查询id
        {
            Literal1.Text = "<span style=\"font-size: 24px;font-weight: bold;\">请选择左边相关层级进行管理</span>";
            Button1.Visible = false;
        }
        else
        {
            if (HiddenField2.Value == null || HiddenField2.Value == "1")
            {
                HiddenField2.Value = "";
                DataTable dt = MeasurementInfoDataDBOption.SelectByFrameID(Guid.Parse(HiddenField1.Value));
                GridView1.Columns[0].Visible = true;
                GridView1.DataSource = dt;
                GridView1.DataBind();
                GridView1.Columns[0].Visible = false;
            }
        }
    }
    void AddFrameLevel(TreeNode tn, int Level, Guid SuperiorID)
    {
        if (Level > int.Parse(ConfigurationManager.AppSettings.Get("FrameLevelLimit")))
            return;
        DataTable dt = SystemFrameworkDataDBOption.SelectByLevel(SuperiorID, Level);
        foreach (DataRow dr in dt.Rows)
        {
            SystemFrameworkData sfd = SystemFrameworkDataFactory.Construct(dr);
            TreeNode tn1 = new TreeNode(sfd.Name, sfd.ID.ToString());//, "", "", url+sfd.ID.ToString(), "_parent");
            if (Level != int.Parse(ConfigurationManager.AppSettings.Get("FrameLevelLimit")))
                tn1.SelectAction = TreeNodeSelectAction.None;
            AddFrameLevel(tn1, Level + 1, sfd.ID);            
            tn.ChildNodes.Add(tn1);
        }
    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (TreeView1.SelectedNode != null)
        {
            string id = TreeView1.SelectedNode.Value;
            HiddenField1.Value = id;
            if (id == null || id == "")//没有查询id
            {
                Literal1.Text = "<span style=\"font-size: 24px;font-weight: bold;\">请选择左边相关层级进行管理</span>";
                Button1.Visible = false;
            }
            else
            {
                Button1.Visible = true;
                DataTable dt;
                if (Guid.Parse(id) == Guid.Empty)
                {
                    Literal1.Text = "<span style=\"font-size: 16px;font-weight: bold;\">当前位置：</span>" + "<span style=\"font-size: 24px;font-weight: bold;\">系统平台</span>";
                    Button1.Attributes.Add("onclick", "javascript:showdialog('" + id + "',''" + ")");
                }
                else
                {
                    dt = MeasurementInfoDataDBOption.SelectByFrameID(Guid.Parse(id));
                    GridView1.Columns[0].Visible = true;
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                    GridView1.Columns[0].Visible = false;
                    SystemFrameworkData sfd = SystemFrameworkDataDBOption.SelectByID(Guid.Parse(id));
                    dt = SystemFrameworkDataDBOption.SelectByLevel(sfd.ID, sfd.FrameLevel + 1);
                    Literal1.Text = "<span style=\"font-size: 16px;font-weight: bold;\">当前位置：</span>" + "<span style=\"font-size: 24px;font-weight: bold;\">" + sfd.Name + "</span>";
                    //                     TextBox1.Text = sfd.Name;
                    //                     TextBox2.Text = sfd.Contact;
                    //                     TextBox3.Text = sfd.Organization;
                    //                     TextBox4.Text = sfd.Address;
                    //                     TextBox5.Text = sfd.Telephone;
                    //                     TextBox6.Text = sfd.Remark;
                    //                     TextBox7.Text = sfd.Creator;
                    //                     TextBox8.Text = sfd.CreateTime.ToString();
                    //                     TextBox9.Text = sfd.UpdateTime.ToString();
                    //                     TextBox10.Text = sfd.MeasurementCountLimit.ToString();
                    //                     TextBox11.Text = sfd.CurrentMeasurementCount.ToString();
                    Button1.Attributes.Add("onclick", "javascript:showdialog('" + id + "','')");
                    //                     Button2.Attributes.Add("onclick", "javascript:showdialog('" + sfd.SuperiorID.ToString() + "','" + sfd.ID.ToString() + "')");
                    //                     if (sfd.FrameLevel > int.Parse(ConfigurationManager.AppSettings.Get("FrameLevelLimit")))
                    //                         Button1.Enabled = false;
                }
            }
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string id = GridView1.Rows[e.NewEditIndex].Cells[0].Text;
        Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>showdialog('" + HiddenField1.Value + "','" + id + "')</script>");
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string id = e.Row.Cells[0].Text;
            ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "showdialog('" + HiddenField1.Value + "','" + id + "')");
            ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "if(!window.confirm('确定删除此用户')) return;");
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = GridView1.Rows[e.RowIndex].Cells[0].Text;
        MeasurementInfoDataDBOption.DeleteByID(Guid.Parse(id));
        Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('删除成功');</script>");
        DataTable dt = MeasurementInfoDataDBOption.SelectByFrameID(Guid.Parse(HiddenField1.Value));
        GridView1.Columns[0].Visible = true;
        GridView1.DataSource = dt;
        GridView1.DataBind();
        GridView1.Columns[0].Visible = false;   
    }
}